package m3f.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import m3f.matrix.SparseVector;

public class SparseImageCLEFMatrixReader extends MatrixReader{
	
	private File file;
	private int vectorSize;
	private int totalRows; 
	BufferedReader reader;
	private String line;
	
	public SparseImageCLEFMatrixReader(){
		vectorSize = 0;
		totalRows = 0;
	}
	
	@Override
	public ArrayList<SparseVector> readVectors(int numVectors){
		ArrayList<SparseVector> vectors = new ArrayList<SparseVector>();
		try{
			long start = System.currentTimeMillis();
			if(reader == null){
				FileInputStream stream = new FileInputStream(file);
				reader = new BufferedReader(new InputStreamReader(stream));
				line = null;
				reader.readLine();
			}
			int vectorCounter = 0;
			if(line != null){
				vectors.add(parseRow(line));
				vectorCounter++;
			}
			while((line = reader.readLine()) != null && vectorCounter < numVectors){
				vectors.add(parseRow(line));
				vectorCounter++;
			}
			if(line == null){
				reader.close();
				reader = null;
				System.out.println(" Closing file");
			}
			//System.out.println(vectors.size() + " vectors read from "+file+" (" + (System.currentTimeMillis()-start) + "ms)");
		}catch(IOException ex){
			ex.printStackTrace();
		}
		return vectors;
	}
	
	private SparseVector parseRow(String row){
		SparseVector v = new SparseVector();
		StringTokenizer stk = new StringTokenizer(row);
		stk.nextToken();
		while(stk.hasMoreTokens()){
			v.setQuick(Integer.valueOf(stk.nextToken()), Double.valueOf(stk.nextToken()));
		}
		if(normalizing()) v.normalizeL1();
		return v;
	}
	
	public static void main(String[] args){
		String filename = "/Users/macbook/Workspace/ImageCLEF2013/webupv13_train_visual_getlf_english_15.feat";
		MatrixReader mr = new SparseImageCLEFMatrixReader();
		mr.normalizeVectors(true);
		mr.start(filename);
		ArrayList<SparseVector> vectors = mr.readVectors(2);
		
	}
	
	
	@Override
	public void start(String filename){
		file = new File(filename);
		try {
			FileInputStream stream = new FileInputStream(file);
			reader = new BufferedReader(new InputStreamReader(stream));

			int minIdx = Integer.MAX_VALUE;
			long start = System.currentTimeMillis();
			String line = reader.readLine();
			String info[] = line.split(" ");
			vectorSize = Integer.valueOf(info[1]);
			totalRows = Integer.valueOf(info[0]);
			System.out.println(totalRows + " vectors with " + (vectorSize) + 
					" dimensions found ["+minIdx+"-based index] in " + file.getName() + " (" 
					+ (System.currentTimeMillis()-start) + "ms)");
			reader.close();
			reader = null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
	}
	
	@Override
	public int getMatrixRows(){
		return totalRows;
	}
	
	@Override
	public int getMatrixColumns(){
		return vectorSize;
	}
}
